package org.bigmarketapi.gatewaycenter.infrastructure.persistent.repository;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.bigmarketapi.gatewaycenter.domain.manage.model.vo.GwServerDetailVO;
import org.bigmarketapi.gatewaycenter.domain.manage.model.vo.GwServerVO;
import org.bigmarketapi.gatewaycenter.domain.manage.repository.ImanageRepository;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppInterfaceMethodVO;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppInterfaceVO;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppSystemVO;
import org.bigmarketapi.gatewaycenter.domain.registry.repository.registryRepository;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.dao.*;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.po.*;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @BelongsProject: Gateway-Center
 * @BelongsPackage: org.bigmarketapi.gatewaycenter.infrastructure.persistent.repository
 * @Author: dujiaqi
 * @CreateTime: 2024-11-30  20:36
 * @Description: TODO
 * @Version: 1.0
 */
@Repository
@Slf4j
public class GwRepositoryImpl implements ImanageRepository, registryRepository {
    GwServerDao gwServerDao;
    GwServerDetailDao gwServerDetailDao;
    AppInterfaceDao appInterfaceDao;
    AppSystemDao appSystemDao;
    AppInterfaceMethodDao appInterfaceMethodDao;
    GwDistributeDao gwDistributeDao;

    public GwRepositoryImpl(GwServerDao gwServerDao, GwServerDetailDao gwServerDetailDao, AppInterfaceDao appInterfaceDao, AppSystemDao appSystemDao, AppInterfaceMethodDao appInterfaceMethodDao, GwDistributeDao gwDistributeDao) {
        this.gwServerDao = gwServerDao;
        this.gwServerDetailDao = gwServerDetailDao;
        this.appInterfaceDao = appInterfaceDao;
        this.appSystemDao = appSystemDao;
        this.appInterfaceMethodDao = appInterfaceMethodDao;
        this.gwDistributeDao = gwDistributeDao;
    }

    @Override
    public List<GwServerVO> queryGatewayServerLIst() {
        final List<GwServer> gwServers = gwServerDao.queryGatewayServerLIst();
        List<GwServerVO> list = new ArrayList<>();
        for (GwServer gwServer : gwServers) {
            GwServerVO gwServerVO = new GwServerVO();
            gwServerVO.setGroupId(gwServer.getGroupId());
            gwServerVO.setGroupName(gwServerVO.getGroupName());
        }
        return list;
    }

    @Override
    public boolean registerGatewayServerNode(String groupId, String gatewayId, String gatewayName, String gatewayAddress, Integer available) {
        GwServerDetail detail = new GwServerDetail();
        detail.setGroupId(groupId);
        detail.setGatewayId(gatewayId);
        detail.setGatewayName(gatewayName);
        detail.setGatewayAddress(gatewayAddress);
        detail.setStatus(String.valueOf(available));
        return gwServerDetailDao.insertGatewayServerNode(detail) == 1;
    }

    @Override
    public GwServerDetailVO queryGatewayServerDetail(String gatewayId, String gatewayAddress) {
        GwServerDetail detail = new GwServerDetail();
        detail.setGatewayId(gatewayId);
        detail.setGatewayAddress(gatewayAddress);
        final GwServerDetail gwServerDetail = gwServerDetailDao.queryGatewayServerDetail(detail);
        if (gwServerDetail == null) {
            return null;
        }
        GwServerDetailVO result = new GwServerDetailVO();
        result.setGatewayId(gwServerDetail.getGatewayId());
        result.setGatewayAddress(gwServerDetail.getGatewayAddress());
        result.setGatewayName(gwServerDetail.getGatewayName());
        result.setStatus(gwServerDetail.getStatus());
        return result;
    }

    @Override
    public boolean updateGatewayStatus(String gatewayId, String gatewayAddress, Integer available) {
        GwServerDetail detail = new GwServerDetail();
        detail.setGatewayId(gatewayId);
        detail.setGatewayAddress(gatewayAddress);
        detail.setStatus(String.valueOf(available));
        return gwServerDetailDao.updateGatewayStatus(detail) == 1;

    }

    @Override
    public void insertApplication(AppSystemVO applicationSystemVO) {
        AppSystem appSystem = new AppSystem();
        appSystem.setSystemId(applicationSystemVO.getSystemId());
        appSystem.setSystemName(applicationSystemVO.getSystemName());
        appSystem.setSystemRegistry(applicationSystemVO.getSystemRegistry());
        appSystem.setSystemType(applicationSystemVO.getSystemType());
        appSystemDao.insertApplication(appSystem);

    }

    @Override
    public void insertApplicationInterface(AppInterfaceVO applicationInterfaceVO) {
        AppInterface appInterface = new AppInterface();
        appInterface.setSystemId(applicationInterfaceVO.getSystemId());
        appInterface.setInterfaceId(applicationInterfaceVO.getInterfaceId());
        appInterface.setInterfaceName(applicationInterfaceVO.getInterfaceName());
        appInterface.setInterfaceVersion(applicationInterfaceVO.getInterfaceVersion());
        appInterfaceDao.insertApplicationInterface(appInterface);
    }

    @Override
    public void insertApplicationInterfaceMethod(AppInterfaceMethodVO applicationInterfaceMethodVO) {
        AppInterfaceMethod appInterfaceMethod = new AppInterfaceMethod();
        appInterfaceMethod.setSystemId(applicationInterfaceMethodVO.getSystemId());
        appInterfaceMethod.setInterfaceId(applicationInterfaceMethodVO.getInterfaceId());
        appInterfaceMethod.setMethodId(applicationInterfaceMethodVO.getMethodId());
        appInterfaceMethod.setMethodName(applicationInterfaceMethodVO.getMethodName());
        appInterfaceMethod.setParameterType(applicationInterfaceMethodVO.getParameterType());
        appInterfaceMethod.setUri(applicationInterfaceMethodVO.getUri());
        appInterfaceMethod.setHttpType(applicationInterfaceMethodVO.getHttpType());
        appInterfaceMethod.setAuth(applicationInterfaceMethodVO.getAuth());
        appInterfaceMethodDao.insertApplicationInterfaceMethod(appInterfaceMethod);
    }

    @Override
    public AppSystemVO queryAppSystemBySystId(String gatewayId) {
        AppSystem system = appSystemDao.queryAppSystemBySystId(gatewayId);
        return AppSystemVO.builder()
                .systemId(system.getSystemId())
                .systemName(system.getSystemName())
                .systemType(system.getSystemType())
                .systemRegistry(system.getSystemRegistry())
                .build();
    }

    @Override
    public List<AppInterfaceVO> queryAppInterfaceBySystemId(String systemId) {
        List<AppInterface> interfaceList = appInterfaceDao.queryAppInterfaceBySystemId(systemId);
        final List<AppInterfaceVO> collects = interfaceList.stream().map(appInterface -> AppInterfaceVO.builder()
                .interfaceId(appInterface.getInterfaceId())
                .interfaceName(appInterface.getInterfaceName())
                .interfaceVersion(appInterface.getInterfaceVersion()).build()).collect(Collectors.toList());
        return collects;
    }

    @Override
    public List<AppInterfaceMethodVO> queryInterMethods(List<String> interfaceIds) {

    /*     final List<CompletableFuture<AppInterfaceMethod>> futures = interfaceIds.stream()
         .map(id -> CompletableFuture.supplyAsync(() -> appInterfaceMethodDao.queryInterfaceMethod(id)))
         .collect(Collectors.toList());
         final List<AppInterfaceMethod> methods = futures.stream()
         .map(CompletableFuture::join)
         .collect(Collectors.toList());*/
      //  final List<AppInterfaceMethod> methods = interfaceIds.stream().map(id -> appInterfaceMethodDao.queryInterfaceMethod(id)).toList();
        final List<AppInterfaceMethod> methods = appInterfaceMethodDao.queryInterfaceMethodList(interfaceIds.get(0));
        log.info("{}", JSON.toJSONString(methods));
        return methods.stream().map(m -> AppInterfaceMethodVO.builder().methodId(m.getMethodId())
                .systemId(m.getSystemId())
                .methodName(m.getMethodName())
                .interfaceId(m.getInterfaceId())
                .auth(m.getAuth())
                .httpType(m.getHttpType())
                .parameterType(m.getParameterType())
                .uri(m.getUri()).build()).toList();

    }

    @Override
    public String querySystemIdByGatewayId(String gatewayId) {
        return gwDistributeDao.querySystemIdByGatewayId(gatewayId);
    }


}
